﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using System.Net;
using System.Text.RegularExpressions;

namespace DSUltimate.Parsers
{
    public class EbayParser
    {
        private const string searchedItemsContainerId = "ListViewInner"; //UL
        private string url = "http://www.ebay.co.uk/sch/i.html?_from=R40&_trksid=p2050601.m570.l1313.TR6.TRC1.A0.H0.X{0}.TRS0&_nkw={0}&_sacat=0";
        WebClient client;

        public EbayParser()
        {
            client = new WebClient();
        }

        public List<EbayItem> SearchForKeywords(string input)
        {
            List<EbayItem> foundItems = new List<EbayItem>();

            //Make Request
            string html = client.DownloadString(string.Format(url, input));
           

            HtmlDocument document = new HtmlDocument();
            document.LoadHtml(html);

            HtmlNode container = document.GetElementbyId(searchedItemsContainerId);
            HtmlNodeCollection htmlItemsFound = container.ChildNodes;

            EbayItem itemToAdd;
            foreach (HtmlNode htmlItem in htmlItemsFound)
            {
                itemToAdd = new EbayItem();
                
                if (htmlItem.OriginalName == "li")
                {
                    //Picture Parse
                    HtmlNode pictureDiv = GetSpecificHtmlElement("div", htmlItem.ChildNodes.ToList<HtmlNode>())[0];
                    string pictureDivHtml = pictureDiv.InnerHtml;
                    Match imgSource = Regex.Match(pictureDivHtml, "<img [^>]*src=\"([^\"]+)\"");
                    itemToAdd.PictureURL = imgSource.Value;

                    HtmlNode title = GetSpecificHtmlElement("h3", htmlItem.ChildNodes.ToList<HtmlNode>())[0]; //Title
                    HtmlNode otherInfoContainer = GetSpecificHtmlElement("ul", htmlItem.ChildNodes.ToList<HtmlNode>())[0];
                    List<HtmlNode> otherInfo = GetSpecificHtmlElement("li", otherInfoContainer.ChildNodes.ToList<HtmlNode>());

                    HtmlNode price = otherInfo[0]; //Price
                    HtmlNode buyItNow = otherInfo[1]; //Buy It Now
                    HtmlNode postage = otherInfo[2]; //Postage
                    HtmlNode soldCount = otherInfo[3]; //SoldCount*/

                    itemToAdd.Title = AmazonParser.RemoveEmptySpaces(title.InnerText);
                    itemToAdd.Price = price.InnerText;
                    itemToAdd.BuyItNow = buyItNow.InnerHtml; //fix
                   // itemToAdd.Postage = postage.InnerHtml;
                    
                    itemToAdd.SoldCount = soldCount.InnerHtml;

                    foundItems.Add(itemToAdd);
                }
            }

            return foundItems;
        }


        private List<HtmlNode> GetSpecificHtmlElement(string elementName, List<HtmlNode> collection)
        {
            List<HtmlNode> foundNodes = new List<HtmlNode>();
            foreach (HtmlNode node in collection)
            {
                if (node.Name == elementName.ToLower())
                {
                    foundNodes.Add(node);
                }
            }
            return foundNodes;
        }
    }
}
